/**
 * 详情页面混入
 */

import DictionarySelect from '@/components/popsoft/DictionarySelect/DictionarySelect'
import DictionaryRadioGroup from '@/components/popsoft/DictionarySelect/DictionaryRadioGroup'
import DictionaryCheckboxGroup from '@/components/popsoft/DictionarySelect/DictionaryCheckboxGroup'
import OrganizationSingleSelect from '@/components/popsoft/CommonSelect/Organization/Single'
import OrganizationMultipleSelect from '@/components/popsoft/CommonSelect/Organization/Multiple'
import UserGroupSingleSelect from '@/components/popsoft/CommonSelect/UserGroup/Single'
import UserSingleSelect from '@/components/popsoft/CommonSelect/User/Single'
import UserMultipleSelect from '@/components/popsoft/CommonSelect/User/Multiple'
import DataDictionarySelect from '@/components/popsoft/CommonSelect/DataDictionary'
import { checkButtonPermission } from '@/utils/auth'
import Tinymce from '@/components/Tinymce'
export const detailMixin = {
  components: {
    DictionarySelect,
    DictionaryRadioGroup,
    DictionaryCheckboxGroup,
    OrganizationSingleSelect,
    OrganizationMultipleSelect,
    UserGroupSingleSelect,
    UserSingleSelect,
    UserMultipleSelect,
    DataDictionarySelect,
    Tinymce
  },
  data() {
    return {
      // 可见性
      visible: false,
      // 加载中
      loading: false,
      // 模式
      mode: '',
      // 只读
      readonly: false

    }
  },
  computed: {
    title() {
      let text = ''
      if (this.mode === 'add') {
        text = '新增'
      } else if (this.mode === 'modify') {
        text = '修改'
      } else if (this.mode === 'view') {
        text = '查看'
      }
      return text
    },
    saveButtonVisible() {
      // 如果是只读模式，直接返回false
      if (this.mode === 'view') {
        return false
      }
      return checkButtonPermission(this.pageCode + this.mode)
    }
  },
  methods: {
    // 新增
    add() {
      this.mode = 'add'
      this.api.init()
        .then(res => {
          this.entityData = res.data
          // 恢复传过来的上级id与name
          if (this.parentId) {
            this.entityData.parentId = this.parentId
            this.entityData.parentName = this.parentName
          }
          if (this.afterAdd) {
            this.afterAdd()
          }
        })

      this.visible = true
    },
    // 修改
    modify(id) {
      this.mode = 'modify'
      if (this.beforeModify) {
        this.beforeModify()
      }
      this.api.get(id)
        .then(res => {
          this.entityData = res.data
          if (this.afterModify) {
            this.afterModify()
          }
        })
      this.visible = true
    },
    // 查看
    view(id) {
      this.mode = 'view'
      this.api.get(id)
        .then(res => {
          this.entityData = res.data
          if (this.afterView) {
            this.afterView()
          }
        })
      this.readonly = true
      this.visible = true
    },
    // 保存
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.beforeSave) {
            this.beforeSave()
          }
          if (this.mode === 'add') {
            this.saveForAdd()
          } else {
            this.saveForModify()
          }
        }
      })
    },
    // 关闭
    close() {
      this.visible = false
    },
    // 新增模式下保存
    saveForAdd() {
      this.api.add(this.entityData)
        .then(res => {
          this.entityData = res.data
          this.$emit('ok', this.mode, this.entityData)
          this.close()
        }).finally(() => {
          this.loading = false
        })
    },
    // 修改模式下保存
    saveForModify() {
      this.api.modify(this.entityData)
        .then(res => {
          this.entityData = res.data
          this.$emit('ok', this.mode, this.entityData)
          this.close()
        }).finally(() => {
          this.loading = false
        })
    }

  }

}
